<template>
  <scroll-view  
    class="position-collection"
    scroll-y
    style="height: 100vh; padding: 20rpx; box-sizing: border-box;"
    @scrolltolower="loadMore"
  >
    <!-- 职位列表 -->
    <view 
      class="position-item"
      v-for="position of collectedPositions"
      :key="position.id"
    >
      <!-- 职位卡片与删除按钮的容器 -->
      <view class="position-container" 
        @touchstart="start($event, position)"
        @touchmove="move($event, position)"
        @touchend="end(position)"
        :style="{transform: `translateX(${position.moveX}px)`}"
      >
        <!-- 职位卡片 -->
        <view class="position-card">
          <!-- 职位内容（保持不变） -->
          <view class="position-card__header">
            <text class="position-card__title">{{ position.positionName }}</text>
            <text class="position-card__salary">{{ position.salary }}</text>
          </view>
          <text class="position-card__company">{{ position.companyName }}</text>
          <view class="position-card__footer">
            <view class="position-card__hr">
              <image :src="position.hrAvatarUrl" class="position-card__hr-avatar"></image>
              <text class="position-card__hr-name">{{ position.respPerson }} · {{ position.respPos }}</text>
            </view>
            <text class="contact-city">{{ position.city }}</text>
          </view>
        </view>
        
        <!-- 删除按钮 -->
        <view class="btn_del" @click="del(position.id)">
          删除
        </view>
      </view>
    </view>

    <!-- 加载完成提示 -->
    <view class="load-complete" v-if="isLoadComplete">已加载全部</view>
  </scroll-view>
</template>

<script>
import { getJobCollect, deleteJobCollect } from "@/api/job.js";

export default {
  data() {
    return {
      delBtnWidth: 160, // 将px改为rpx单位 (80px ≈ 160rpx)
      collectedPositions: [],
      pageSize: 10,
      pageNum: 1,
      isLoadComplete: false  // 仅保留加载完成状态
    };
  },
  methods: {
    async fetchCollectedPositions() {
      try {
        const res = await getJobCollect(this.pageNum, this.pageSize);
        if (res.code === 200) {
          const newList = res.data.list || [];
          // 初始加载直接赋值，加载更多追加数据
          if (this.pageNum === 1) {
            this.collectedPositions = newList;
          } else {
            this.collectedPositions = [...this.collectedPositions, ...newList];
          }
          // 为新数据添加滑动相关属性
          newList.forEach(position => {
            this.$set(position, "moveX", 0);
            this.$set(position, "startX", 0);
            this.$set(position, "isMoving", false);
          });
          
          // 关键修改：只要返回的数据量小于页大小，就说明没有更多数据了
          if (newList.length < this.pageSize) {
            this.isLoadComplete = true;
          }
        } else {
          console.error("获取收藏职位失败：", res.message);
        }
      } catch (error) {
        console.error("获取收藏职位出错：", error);
      }
    },
    // 滑动相关方法（保持不变）
    start(e, position) {
      position.startX = e.touches[0].clientX;
      position.isMoving = true;
      this.collectedPositions.forEach(p => {
        if (p.id !== position.id && p.moveX < 0) p.moveX = 0;
      });
    },
    move(e, position) {
      if (!position.isMoving) return;
      const currentX = e.touches[0].clientX;
      const diffX = currentX - position.startX;
      position.moveX = Math.max(Math.min(diffX, 0), -this.delBtnWidth);
    },
    end(position) {
      position.isMoving = false;
      position.moveX = position.moveX <= -this.delBtnWidth / 2 ? -this.delBtnWidth : 0;
    },
    // 删除收藏的职位
    async del(positionId) {
      try {
        const res = await deleteJobCollect(positionId);
        if (res.code === 200) {
          this.collectedPositions = this.collectedPositions.filter(item => item.id !== positionId);
        } else {
          console.error("删除收藏职位失败：", res.message);
        }
      } catch (error) {
        console.error("删除收藏职位出错：", error);
      }
    },
    // 触底加载更多
    loadMore() {
      if (!this.isLoadComplete) {
        this.pageNum += 1;
        this.fetchCollectedPositions();  // 复用获取数据方法
      }
    }
  },
  mounted() {
    this.fetchCollectedPositions();  // 初始加载
  }
};
</script>

<style scoped>
/* 页面容器基础样式 */
.position-collection {
  background-color: #f5f5f5;
  padding: 20rpx; /* 调整为统一的 20rpx padding */
  box-sizing: border-box;
}

/* 单个职位项（外层容器，控制显示范围） */
.position-item {
  margin-bottom: 20rpx;
  overflow: hidden;
  border-radius: 12rpx;
}

/* 职位容器（卡片+删除按钮的父容器） */
.position-container {
  display: flex;
  width: calc(100% + 160rpx); /* 恢复calc宽度，使用rpx单位 */
  transition: transform 0.3s ease;
  position: relative;
}

/* 删除按钮（关键样式） */
.btn_del {
  width: 160rpx; /* 将px改为rpx单位 */
  background: #ff3b30;
  color: #fff;
  font-size: 32rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
}

/* 职位卡片样式 */
.position-card {
  background-color: #ffffff;
  padding: 30rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  width: 100%;
  box-sizing: border-box;
}

/* 卡片内部样式（保持不变） */
.position-card__header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 20rpx;
  width: 81%;
}
.position-card__title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
}
.position-card__salary {
  font-size: 33rpx;
  color: #007aff; 
}
.position-card__company {
  font-size: 28rpx;
  color: #666;
  margin-bottom: 25rpx;
  display: block;
}
.position-card__footer {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-top: 10rpx;
}
.position-card__hr {
  display: flex;
  align-items: center;
}
.position-card__hr-avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
  margin-right: 15rpx;
}
.position-card__hr-name {
  font-size: 26rpx;
  color: #666;
}
.contact-city {
  margin-left: auto;  /* 补充城市文本的右对齐样式 */
  font-size: 26rpx;
  color: #666;
}

/* 加载完成提示样式 */
.load-complete {
  text-align: center;
  font-size: 24rpx;
  color: #999;
  padding: 20rpx;
}
</style>